﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Rem.Objects;

namespace FileDB
{

    public partial class Reminder
    {
        public Reminder()
        {
            this.Id = QueryConstants.UnsavedObjectId;
            this.Message = "请输入提醒信息";
            this.StartTime = DateTime.Now.Date;
            this.DueTime = DateTime.Now.Date;//  DateTime.Now.AddYears(1);
            this.ReminderTime = DateTime.Now.Date;
            this.CreatedTime = DateTime.Now.Date;
            this.Category = ReminderCategory.GetReminder;
            this.Status = ReminderState.Actived;
        }

        public int Id { get; set; }
        public DateTime StartTime { get; set; }
        public DateTime DueTime { get; set; }

        public string Message { get; set; }
        public string Status { get; set; }

        public DateTime ReminderTime { get; set; }
        public string ReminderSound { get; set; }

        public DateTime CreatedTime { get; set; }
        public int ContractId { get; set; }

        public string Category { get; set; }
        public string BatchId { get; set; }
        public string ReminderTimeString
        {
            get
            {
                return this.ReminderTime.ToString("yyyy-MM-dd");
            }
        }

        public static object CurrentReminders()
        {
            throw new NotImplementedException();
        }

        public static object ThisMonthReminders()
        {
            throw new NotImplementedException();
        }

        public static object NotFinishedReminders()
        {
            List<Reminder> reminders = new List<Reminder>();
            string[] files = Directory.GetFiles(FullRootDir, string.Format("{0}-?-{1}-{2}", (int)RState.Actived, "*", "*"));
            foreach (var file in files)
            {
                reminders.Add(Reminder.GetReminder(file));
            }
            return (from r in reminders
                    where (r.ReminderTime.Date <= DateTime.Now.Date)
                    orderby r.ReminderTime
                    select r ).ToList();
        }

        public static object PostRentReminders()
        {
            List<Reminder> reminders = new List<Reminder>();
            string[] files = Directory.GetFiles(FullRootDir, string.Format("?-{0}-{1}-{2}", (int)RCategory.PostReminder, "*", "*"));
            foreach (var file in files)
            {
                reminders.Add(Reminder.GetReminder(file));
            }
            return (from r in reminders
                    where r.ReminderTime.Year == DateTime.Now.Year
                    && r.ReminderTime.Month == DateTime.Now.Month
                    orderby r.ReminderTime
                    select r).ToList();
        }

        public static List<Reminder> GetRentReminders()
        {
            List<Reminder> reminders = new List<Reminder>();
            string[] files = Directory.GetFiles(FullRootDir, string.Format("?-{0}-{1}-{2}", (int)RCategory.GetReminder, "*", "*"));
            foreach (var file in files)
            {
                reminders.Add(Reminder.GetReminder(file));
            }
            return (from r in reminders
                    where r.ReminderTime.Year == DateTime.Now.Year
                    && r.ReminderTime.Month == DateTime.Now.Month
                    orderby r.ReminderTime
                    select r).ToList();
        }

        public static object ContractReminders()
        {
            throw new NotImplementedException();
        }

        public static object GetImportantReminders()
        {
            List<Reminder> reminders = new List<Reminder>();
            string[] files = Directory.GetFiles(FullRootDir, string.Format("?-{0}-{1}-{2}", (int)RCategory.ImportantReminder, "*", "*"));
            foreach (var file in files)
            {
                reminders.Add(Reminder.GetReminder(file));
            }
            return (from r in reminders
                    orderby r.ReminderTime
                    select r).ToList();
        }

        public static object GetRenewReminders()
        {
            List<Reminder> reminders = new List<Reminder>();
            string[] files = Directory.GetFiles(FullRootDir, string.Format("?-{0}-{1}-{2}", (int)RCategory.RenewReminder, "*", "*"));
            foreach (var file in files)
            {
                reminders.Add(Reminder.GetReminder(file));
            }
            return (from r in reminders
                    orderby r.ReminderTime
                    select r).ToList();
        }

        public static void DeleteContractReminders(int contractId)
        {
            List<string> files = GetContractReminders(contractId);
            foreach (var file in files)
            {
                File.Delete(file);
            }
        }

        public static List<string> GetContractReminders(int contractId)
        {
            var fileList = Directory.GetFiles(FullRootDir, string.Format("*-*-{0}-*.xml", contractId.ToString("d8")));
            return fileList.ToList();
        }


        public bool Delete()
        {
            Reminder.Delete(this);
            return true;
        }
        public double RelatedRent { get; set; }
    }
}

